package com.zHan.sky.controller.User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zHan.sky.common.contant.StatusConstant;
import com.zHan.sky.common.result.Result;
import com.zHan.sky.entity.Category;
import com.zHan.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分类管理
 */
@RestController("userCategoryController")
@Slf4j
@RequestMapping("/user/category")
@Api(tags = "C端-分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据类型查询分类数据
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询分类数据")
    public Result<List<Category>> list(Category category) {
        //创建条件构造器对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        Integer categoryType = category.getType();
        queryWrapper.eq(categoryType != null, Category::getType, categoryType)
                .eq(Category::getStatus, StatusConstant.ENABLE);
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //执行方法
        List<Category> categories = categoryService.list(queryWrapper);

        return Result.success(categories);
    }

}
